package com.wang.mall.listener;

import com.rabbitmq.client.Channel;
import com.wang.mall.bean.OmsOrder;
import com.wang.mall.service.OmsOrderService;
import com.wang.mall.util.AlipayTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 监听订单关闭队列
 */
@Service
@RabbitListener(queues = "wang-order-release-order-queue")
public class OrderCloseListener {

    @Autowired
    AlipayTemplate alipayTemplate;

    @Autowired
    OmsOrderService omsOrderService;

    @RabbitHandler
    public void listenerOrderClose(OmsOrder omsOrder, Channel channel, Message message) throws IOException {
        System.out.println("收到过期订单信息");
        try {
            omsOrderService.closeOrder(omsOrder);
            // 手动调用支付宝收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
